﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using DiagramDesigner.Common.Controls;

namespace DiagramDesigner.Module.Toolbox.Controls
{
    public class ToolboxItem : ContentControl
    {
        private Point? _dragStartPoint;

        public static readonly DependencyProperty TooltipTextProperty =
            DependencyProperty.Register("TooltipText", typeof (string), typeof (ToolboxItem), new PropertyMetadata(default(string)));

        public string TooltipText
        {
            get { return (string) GetValue(TooltipTextProperty); }
            set { SetValue(TooltipTextProperty, value); }
        }

        protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseDown(e);
            if (this.Content as DesignerItem == null) return;
            _dragStartPoint = new Point?(e.GetPosition(this));
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (e.LeftButton != MouseButtonState.Pressed)
            {
                this._dragStartPoint = null;
            }

            if (this._dragStartPoint.HasValue)
            {
                
                Point position = e.GetPosition(this);
                if ((SystemParameters.MinimumHorizontalDragDistance <=
                     Math.Abs((double) (position.X - this._dragStartPoint.Value.X))) ||
                    (SystemParameters.MinimumVerticalDragDistance <=
                     Math.Abs((double) (position.Y - this._dragStartPoint.Value.Y))))
                {
                    string xamlString = XamlWriter.Save(this.Content);
                    DataObject dataObject = new DataObject("DESIGNER_ITEM", xamlString);
                    DragDrop.DoDragDrop(this, dataObject, DragDropEffects.Copy);
                }

                e.Handled = true;
            }
        }

    }
}
